package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.PublishConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.system.domain.bo.PCityBo;
import com.ruoyi.system.domain.vo.PCityVo;
import com.ruoyi.system.service.IPCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 城市Controller
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@Validated
@Api(value = "城市控制器", tags = {"城市管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/city")
public class PCityController extends BaseController {

    private final IPCityService iPCityService;

    /**
     * 查询城市列表
     */
    @ApiOperation("查询城市列表")
    @GetMapping("/list")
    public R<List<PCityVo>> list(PCityBo bo) {
        Boolean flag = RedisUtils.hasKey(PublishConstants.REGION_PREFIX);
        if (flag) {
            List<PCityVo> citys = RedisUtils.getCacheObject(PublishConstants.REGION_PREFIX);
            String name = bo.getName();
            if (StringUtils.isNotBlank(name)) {
                return R.ok(citys.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList()));
            }

            return R.ok(citys);
        }

        List<PCityVo> list = iPCityService.queryList(bo);
        RedisUtils.setCacheObject(PublishConstants.REGION_PREFIX, list, Duration.ofHours(2));

        return R.ok(list);
    }

    /**
     * 导出城市列表
     */
    @ApiOperation("导出城市列表")
    @SaCheckPermission("system:city:export")
    @Log(title = "城市", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PCityBo bo, HttpServletResponse response) {
        List<PCityVo> list = iPCityService.queryList(bo);
        ExcelUtil.exportExcel(list, "城市", PCityVo.class, response);
    }

    /**
     * 获取城市详细信息
     */
    @ApiOperation("获取城市详细信息")
    @SaCheckPermission("system:city:query")
    @GetMapping("/{id}")
    public R<PCityVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPCityService.queryById(id));
    }

    /**
     * 新增城市
     */
    @ApiOperation("新增城市")
    @SaCheckPermission("system:city:add")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PCityBo bo) {
        return toAjax(iPCityService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改城市
     */
    @ApiOperation("修改城市")
    @SaCheckPermission("system:city:edit")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PCityBo bo) {
        return toAjax(iPCityService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除城市
     */
    @ApiOperation("删除城市")
    @SaCheckPermission("system:city:remove")
    @Log(title = "城市", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPCityService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
